<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="出库单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="saleoutCode">
              <a-input v-model="model.serial" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customId">
              <j-dict-select-tag v-model="model.customId" dict-code="hf_custom,name,id"></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="客户订单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="custom">
              <j-search-select-tag type="list" v-model="model.customOrderSerial" placeholder="请选择客户订单号" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-show="false">
            <a-form-model-item label="源单id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sourceId">
              <a-input v-model="model.sourceId" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单据日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="documentDate">
              <j-date placeholder="请输入单据日期" v-model="model.documentDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="红蓝单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isRed">
              <j-dict-select-tag type="list" v-model="model.isRed" dictCode="hf_if_red" placeholder="请选择红蓝单" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="制单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operator">
              <j-search-select-tag
                v-model="model.operator"
                title="制单"
                dict="sys_user,realname,realname"
                placeholder="请选择用户"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审核员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checker">
              <j-search-select-tag
                v-model="model.operator"
                title="审核员"
                dict="sys_user,realname,realname"
                placeholder="请选择审核员"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审核时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checkTime">
              <j-date placeholder="请选择审核时间" v-model="model.checkTime" style="width: 100%" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注"></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { getAction, httpAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import dayjs from 'dayjs'
import store from '@/store'

export default {
  name: 'HfSaleoutModal',
  components: {},
  data() {
    return {
      title: '操作',
      width: 800,
      visible: false,
      model: {
        operator: store.getters.userInfo.realname,
        documentDate: dayjs().format('YYYY-MM-DD HH:mm:ss'),
        isRed: 0,
        status: 0,
        isClosed: 0
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      validatorRules: {
        customId: [{ required: true, message: '客户不能为空', trigger: 'blur' }],
        documentDate: [{ required: true, message: '单据日期不能为空', trigger: 'blur' }]
      },
      url: {
        add: '/sale/hfSaleOut/add',
        edit: '/sale/hfSaleOut/edit'
      }
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(value, row) {
      this.model.sourceId = row.id
      this.model.sourceSerial = row.serial
    }
  }
}
</script>
